<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="java.math.BigDecimal"%>
<%@page import="inet.util.StringTool"%>
<%@page import="lib.db.clip.Clip"%>
<%@page import="lib.db.clip.ClipCategory"%>
<%@page import="java.text.NumberFormat"%>
<%@page import="java.text.DecimalFormat"%>
<jsp:useBean id="clipDAO" class="lib.db.clip.ClipDAO" scope="session"/>
<jsp:useBean id="categoryDAO" class="lib.db.clip.ClipCategoryDAO" scope="session"/>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru">
	<head>
		<link rel="stylesheet" type="text/css" href="include/style.css" />
		<link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />
		<meta http-equiv="Content-Type" content="application/vnd.wap.xhtml+xml; charset=UTF-8" />
		<title>Clip hay nhat nam 2013</title>
	</head>
	<body>
		<%@ include file="include/header.jsp" %>
		<% 
            String sPage = request.getParameter("page");
	        String sCateId = request.getParameter("cateId");
	        BigDecimal cateId = BigDecimal.ZERO;
	        if(sCateId == null) sCateId = "-1";
	        if(sCateId != null && StringTool.isNumberic(sCateId)) {
	            cateId = new BigDecimal(sCateId);
	        }
	        if(sPage == null){
	            sPage="1"; 
	        }
	        int currPage=1;
	        try{
	            currPage = Integer.parseInt(sPage);
	        }catch(NumberFormatException ex){
	            currPage = 1;
	        }
	        int PagesOfSegment = 5;
	        int rowsPerPage = 1;
	        int totalRecs = 0;
	        int numOfPages = 0;
	        if(cateId != BigDecimal.ZERO) {
	            totalRecs = clipDAO.countByCateId(cateId);
	        } else {
	            totalRecs = clipDAO.countAll();
	        }
	        numOfPages = (totalRecs % rowsPerPage != 0)?(totalRecs / rowsPerPage + 1):(totalRecs / rowsPerPage);
	        if(totalRecs <= rowsPerPage) {
	            numOfPages = 1;
	        }
	        if(totalRecs == 0){ 
	            numOfPages = 0;
	        }
	        if(currPage > numOfPages) {
	            currPage = numOfPages;
	        }
	        if(currPage < 0) {
	            currPage = 1;
	        }
	        Vector vFile = null;
	        if(cateId != BigDecimal.ZERO) {
	            vFile = clipDAO.findByCateId(cateId, currPage, rowsPerPage);
	        } else {
	            vFile = clipDAO.findAll(currPage, rowsPerPage);
	        }
	        if(vFile != null && !vFile.isEmpty()) {
	           Clip clip = null;
	           DecimalFormat df2 = new DecimalFormat( "#,###,###,##0" );
	           for (Iterator it = vFile.iterator(); it.hasNext(); ) {
	               clip = (Clip)it.next();
	               if(clip == null) continue;
		%>
		<div class="rekl">
			<div class="clip">
				<a class="link-tai">
					<img class="border-pic" width="60px" vspace="5" hspace="5" height="60px" align="left" 
					title="Video-clip-Hot-Girl-Ba-Tung-lai-tung-Video-Nong" alt="Video-clip-Hot-Girl-Ba-Tung-lai-tung-Video-Nong" 
					src="http://clip.wap.vn/image-video/Video-clip-HotGirlBaTunglaitungVideoNong-3709.gif" />
				</a>
				<strong style="color: #000000;"><%= clip.getName() %></strong>
				(<%= clip.getDownloadCounter() %> lượt tải)
				<br />
				Dung lượng : <%= df2.format(clip.getClipCapacity()/(double)1024) %> KB
				<br />
				<img vspace="5" hspace="5" align="absmiddle" src="images/download.gif" />
				<a class="link-tai">Tải về </a>
			</div>
			<div class="both"></div>
		</div>
		<%
                } // end for
            } // end if
		%>
        <div style="padding: 2px 4px 4px 4px; text-align: left;">
            <font color="#0000FF">
                <%
                    int startNo = 0;
                    int endNo = 0;
                    if (numOfPages > 1) {
                        //Print Previous Page if required
                        out.println("Trang");
                        int currSegment = currPage / PagesOfSegment;
                        if (currPage % PagesOfSegment > 0)
                            currSegment += 1;
                        startNo = currSegment * PagesOfSegment - PagesOfSegment + 1;
                        if (currSegment * PagesOfSegment > numOfPages)
                            endNo = numOfPages;
                        else
                            endNo = currSegment * PagesOfSegment;
                        if (currSegment > 1) {
                            out.print("<a href='?page=1&cateId="+ sCateId +"' class='bluedark-text'> Đầu </a>");
                            out.print("<a  href='?page=" + (currSegment - 1)
                                    * PagesOfSegment + "&cateId="+ sCateId +"' class='bluedark-text'> &lt;&lt;</a>");
                        }
                        //Print direct access page numbers.
                        for (int No = startNo; No <= endNo; No++) {
                            if (No == currPage) {
                                out.print("<font color='red'><b>" + No + "</b></font>  ");
                            } else {
                                out.print("<a href='?page=" + No + "&cateId="+ sCateId +"' class='bluedark-text'>" + No + "</a>  ");
                            }
                        }
                        //Print Next Page if required
                        if (numOfPages != endNo)
                            out.print(" ...<a href='?page=" + numOfPages
                                    + "&cateId="+ sCateId +"' class='bluedark-text'> " + numOfPages + " </a>");
        
                        if (numOfPages > PagesOfSegment * currSegment) {
                            out.print("<a href='?page=" + ((currSegment * PagesOfSegment) + 1)
                                    + "&cateId="+ sCateId +"' class='bluedark-text'>&gt;&gt;</a>");
                            out.print("<a href='?page=" + numOfPages + "&cateId="+ sCateId +"' class='bluedark-text'> Cuối </a>");
                        }
                    }
                %>
            </font>
        </div>
		<div class="content">
	        <% 
	            Vector vCate = categoryDAO.findAll(1, 50);
	            if(vCate != null && !vCate.isEmpty()) {
	               ClipCategory cate = null;
	               for (Iterator it = vCate.iterator(); it.hasNext(); ) {
	                   cate = (ClipCategory) it.next();
	                   if(cate == null) continue;
	        %>
	        <img src="images/category.gif" alt="" />
            <a href="?cateId=<%=cate.getId() %>"><%= cate.getName() %></a>
            <hr />
	        <%
	               }
	            }
	        %>
		</div>

		<div class="banners">
			<img src="http://wapdiz.ru/down/files/graphic/banners/free_load.gif"
				alt="banner" />
		</div>

		<%@ include file="include/footer.jsp" %>

		<div class="stat">
			<a href="http://waplog.net/ru/c.shtml?47916"><img
					src="http://c.waplog.net/ru/47916.cnt" alt="waplog" />
			</a>
		</div>

	</body>
</html>
